package com.typy.www.tpay.ui.acitivity;

import android.content.Context;
import android.content.QuickViewConstants;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.typy.www.tpay.LoginActivity;
import com.typy.www.tpay.R;
import com.typy.www.tpay.base.BaseBackActivity;
import com.typy.www.tpay.bean.bean.BaseReturn;
import com.typy.www.tpay.bean.listvm.NoticeListVm;
import com.typy.www.tpay.bean.vm.UserInfoVm;
import com.typy.www.tpay.constance.Constance;
import com.typy.www.tpay.constance.MessageEvent;
import com.typy.www.tpay.constance.Sharep;
import com.typy.www.tpay.ui.prt.PtrScrollContent;
import com.typy.www.tpay.ui.view.CircleImageView;
import com.typy.www.tpay.util.ClickFastUtil;
import com.typy.www.tpay.util.GsonUtils;
import com.typy.www.tpay.util.MessageAction;
import com.typy.www.tpay.util.ParamsUtil;
import com.typy.www.tpay.util.PreferencesUtil;
import com.typy.www.tpay.util.StringUtil;
import com.typy.www.tpay.util.UiHelper;
import com.typy.www.tpay.util.UpdataUiUtil;
import com.typy.www.tpay.util.listviewanimations.IntentUtil;
import com.typy.www.tpay.volley.HTTPUtils;
import com.typy.www.tpay.volley.VolleyListener;
import com.zhy.android.percent.support.PercentLinearLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 13799 on 2018/6/11.
 */

public class SettingActivity extends BaseBackActivity {

    PtrScrollContent ptrScrollContent;
    @BindView(R.id.tv_touxian)
    CircleImageView tvTouxian;
    @BindView(R.id.ll_touxian)
    PercentLinearLayout llTouxian;
    @BindView(R.id.tv_nickname)
    TextView tvNickname;
    @BindView(R.id.ll_nickname)
    PercentLinearLayout llNickname;
    @BindView(R.id.tv_uid)
    TextView tvUid;
    @BindView(R.id.ll_uid)
    PercentLinearLayout llUid;
    @BindView(R.id.tv_xuliehao)
    TextView tvXuliehao;
    @BindView(R.id.ll_xuliehao)
    PercentLinearLayout llXuliehao;
    @BindView(R.id.ll_mybankcard)
    PercentLinearLayout llMybankcard;
    @BindView(R.id.ll_denglumima)
    PercentLinearLayout llDenglumima;
    @BindView(R.id.ll_zhifumima)
    PercentLinearLayout llZhifumima;
    @BindView(R.id.ll_tousujianyi)
    PercentLinearLayout llTousujianyi;
    @BindView(R.id.ll_banben)
    PercentLinearLayout llBanben;
    @BindView(R.id.ll_guanyu)
    PercentLinearLayout llGuanyu;
    @BindView(R.id.ll_gonggao)
    PercentLinearLayout llGonggao;
    UserInfoVm userInfoVm;
    @BindView(R.id.tv_grade)
    TextView tvGrade;
    @BindView(R.id.tv_logout)
    TextView tvLogout;
    NoticeListVm listVm;
    String aboutUrl;
    String versionUrl;
    @BindView(R.id.tv_qiandao)
    TextView tvQiandao;

    public static void show(Context context) {
        IntentUtil.intent2Activity(context, SettingActivity.class);
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        setTitle("设置");
        refresh();
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void MessageEventBus(MessageEvent msg) {
        if (msg.getKey().equals(MessageAction.REFRESHUSERINFO)) {
           refresh();
        }
    }

    @Override
    protected View getContentView() {
        ptrScrollContent = new PtrScrollContent(this, R.layout.activity_setting) {
            @Override
            protected void getPage(int pageIndex, boolean refresh) {
                refresh();
            }
        };
        return ptrScrollContent.getmView();
    }
    private void refresh(){
        getData();
        getAboutData();
        getVersionData();
    }
    private void updataUserInfo() {

        if (userInfoVm != null)
            UpdataUiUtil.getDefaul().setText(userInfoVm.getFdNickName(), tvNickname)
                    .setText("UID：" + userInfoVm.getFdId(), tvUid).setText(userInfoVm.getFdPayAddress(), tvXuliehao)
                    .setText("等级：" + userInfoVm.getFdLevelName(), tvGrade);
        setYiqian(userInfoVm.isFdIsSign());
    }

    private void setYiqian(boolean isSing) {
        if(isSing) {
            tvQiandao.setBackgroundResource(R.drawable.border_corner_blue);
            tvQiandao.setTextColor(getResources().getColor(R.color.blue67BCFD));
            tvQiandao.setText(R.string.yiqiandao);
            tvQiandao.setClickable(false);
        }else{
            tvQiandao.setBackgroundResource(R.drawable.btn_corner_defuat);
            tvQiandao.setTextColor(getResources().getColor(R.color.white));
            tvQiandao.setText(R.string.qiandao);
        }
    }


    @OnClick({R.id.ll_touxian, R.id.ll_nickname, R.id.ll_uid, R.id.ll_xuliehao, R.id.ll_mybankcard, R.id.ll_denglumima, R.id.ll_zhifumima,
            R.id.ll_tousujianyi, R.id.ll_banben, R.id.ll_guanyu, R.id.ll_gonggao, R.id.tv_logout, R.id.tv_qiandao})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_touxian:
//                DetailInfomationActivity.show(getActivity());
                break;
            case R.id.ll_nickname:
                ChangePasswordActivity.show(this, Constance.CHANGENICKNAME, userInfoVm.getFdNickName());
                break;
            case R.id.ll_uid:
                break;
            case R.id.ll_xuliehao:

                break;
            case R.id.ll_mybankcard:
                MyBankCardActivity.show(getActivity());
                break;
            case R.id.ll_denglumima:
                ChangePasswordActivity.show(getActivity(), Constance.CHANGEPASSWORD);
                break;
            case R.id.ll_zhifumima:
                ChangePasswordActivity.show(getActivity(), Constance.CHANGEPAYPASSWORD);
                break;
            case R.id.ll_tousujianyi:
                SuggestActivity.show(getActivity());
                break;
            case R.id.ll_banben:
//                VersionActivity.show(getActivity(),Constance.VERSIONACTIVITY);
                if (!StringUtil.isEmpty(aboutUrl)) {
                    WebActivity.show(getActivity(), "版本", versionUrl);
                } else {
                    UiHelper.showToast(getActivity(), "未获取到数据请刷新重试！");
                }
                break;
            case R.id.ll_guanyu:
                if (!StringUtil.isEmpty(aboutUrl)) {
                    WebActivity.show(getActivity(), "关于", aboutUrl);
                } else {
                    UiHelper.showToast(getActivity(), "未获取到数据请刷新重试！");
                }
                break;
            case R.id.ll_gonggao:
                NoticeActivity.show(getActivity());
                break;

            case R.id.tv_logout:
                doLogout();
                break;
            case R.id.tv_qiandao:
                if(ClickFastUtil.isFastClick1000())
                doSign();
                break;
        }
    }


    //获取个人信息
    private void getData() {
        ParamsUtil params = new ParamsUtil();
        HTTPUtils.post(this, Constance.getMyselfinfoURL(), params.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                userInfoVm = GsonUtils.parseJSON(ret.getData().toString(), UserInfoVm.class);
                updataUserInfo();
                ptrScrollContent.loadInitialPage();
            }

            @Override
            public void onResponseErr(int code, String msg) {
                UiHelper.showToast(getActivity(), msg);
            }
        });
    }

    //退出登录
    private void doLogout() {
        ParamsUtil params = new ParamsUtil();
        HTTPUtils.post(this, Constance.getLogoutURL(), params.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                UiHelper.showToast(getActivity(), "成功退出登录", true);
                PreferencesUtil.save(Sharep.TOKEN, "");
                PreferencesUtil.save(Sharep.PASSWORD, "");
                PreferencesUtil.save(Sharep.REFRESHTOKEN, "");
                LoginActivity.show(getActivity());
                getActivity().finish();
            }

            @Override
            public void onResponseErr(int code, String msg) {
                UiHelper.showToast(getActivity(), msg);
            }
        });
    }//签到
    private void doSign() {
        ParamsUtil params = new ParamsUtil();
        HTTPUtils.post(this, Constance.getSignURL(), params.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
               refresh();
            }

            @Override
            public void onResponseErr(int code, String msg) {
                UiHelper.showToast(getActivity(), msg);
            }
        });
    }

    //获取关于
    private void getAboutData() {
        ParamsUtil params = new ParamsUtil();
        HTTPUtils.post(this, Constance.getAboutURL(), params.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                if (ret.getCode() == 200 && ret.getData() != null) {
                    listVm = GsonUtils.parseJSON(ret.getData().toString(), NoticeListVm.class);
                    if (listVm != null && listVm.getData() != null && listVm.getData().size() != 0) {
                        aboutUrl = listVm.getData().get(0).getFdUrl();
                    }
                }
            }

            @Override
            public void onResponseErr(int code, String msg) {
                UiHelper.showToast(getActivity(), msg);
            }
        });
    }//获取版本

    private void getVersionData() {
        ParamsUtil params = new ParamsUtil();
        HTTPUtils.post(this, Constance.getVersionURL(), params.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                if (ret.getCode() == 200 && ret.getData() != null) {
                    listVm = GsonUtils.parseJSON(ret.getData().toString(), NoticeListVm.class);
                    if (listVm != null && listVm.getData() != null && listVm.getData().size() != 0) {
                        versionUrl = listVm.getData().get(0).getFdUrl();
                    }
                }
            }

            @Override
            public void onResponseErr(int code, String msg) {
                UiHelper.showToast(getActivity(), msg);
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

}
